import { getActivityList } from "../../utils/activity";
Page({
  data: {
    activeFilter: 'workload',
    searchKeyword: '',
    totalActivity: 16585,
    hasSearched: false,
    isSearchFocused: false,
    filteredResults: []
  },

  onLoad: function () {
    //加载数据
    this.loadActivityData();
  },
  //加载活动列表
  loadActivityData: function (callback) {
    // 调用接口
    getActivityList().then(res => {
      console.log(res)
      this.setData({
        filteredResults: res
      })
      if (callback) callback();
    })
  },

  // 切换筛选条件
  switchFilter: function (e) {
    const filter = e.currentTarget.dataset.filter;
    this.setData({
      activeFilter: filter
    });

    // 这里可以根据不同筛选条件处理搜索结果
    console.log('已切换到筛选条件:', filter);
  },

  // 搜索框输入事件
  onSearchInput: function (e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  // 搜索框获取焦点
  onSearchFocus: function () {
    this.setData({
      isSearchFocused: true
    });
    // 添加震动反馈
    wx.vibrateShort({
      type: 'light'
    });
  },

  // 搜索框失去焦点
  onSearchBlur: function () {
    this.setData({
      isSearchFocused: false
    });
  },

  // 执行搜索
  onSearch: function () {
    const keyword = this.data.searchKeyword.trim().toLowerCase();
    let results = this.data.searchResults;

    if (keyword) {
      results = this.data.searchResults.filter(item => {
        return (
          (item.name && item.name.toLowerCase().includes(keyword)) ||
          (item.phone && item.phone.includes(keyword)) ||
          (item.address && item.address.toLowerCase().includes(keyword))
        );
      });
    }

    this.setData({
      filteredResults: results,
      hasSearched: true
    });

    console.log('搜索关键词:', keyword, '结果数量:', results.length);

    // 添加震动反馈
    wx.vibrateShort({
      type: 'medium'
    });
  },

  // 选择活动
  onSelectActivity: function (e) {
    const activityId = e.currentTarget.dataset.id;
    const activity = this.data.filteredResults.find(item => item.uid === activityId);

    if (activity) {
      console.log('已选择活动:', activity);

      // 导航到活动详情页
      wx.navigateTo({
        url: '/pages/activity/detail/detail?id=' + activity.uid,
        fail: function (err) {
          console.error('导航失败', err);
          wx.showToast({
            title: '页面跳转失败',
            icon: 'none',
            duration: 2000
          });
        }
      });
    }
  },
  // 打开地区选择器
  openRegionPicker: function () {
    this.setData({
      showRegionPicker: true,
      activeFilter: 'region'
    });

    // 添加震动反馈
    wx.vibrateShort({
      type: 'light'
    });
  },

  // 关闭地区选择器
  closeRegionPicker: function () {
    this.setData({
      showRegionPicker: false
    });
  },

  // 处理省市县选择器变化
  regionChange: function(e) {
    const region = e.detail.value;
    this.setData({
      region: region,
      province: region[0],
      city: region[1],
      district: region[2]
    });
  },

  // 重置地区选择
  resetRegion: function () {
    this.setData({
      region: ['', '', ''],
      province: '',
      city: '',
      district: '',
      selectedRegionText: ''
    });
  },

  // 确认地区选择
  confirmRegionSelection: function () {
    let regionText = '';

    if (this.data.province) {
      regionText = this.data.province;

      if (this.data.city) {
        regionText += ' ' + this.data.city;

        if (this.data.district) {
          regionText += ' ' + this.data.district;
        }
      }
    }

    this.setData({
      selectedRegionText: regionText,
      showRegionPicker: false,
      searchKeyword: regionText // 将选择的地区信息填充到搜索框中
    });

    // 如果已经选择了地区，触发搜索
    if (regionText) {
      this.onSearch();
    }
  },
  // 显示工作量弹窗
  showWorkloadPopup: function () {
    this.setData({
      showWorkloadPopup: true,
      activeFilter: 'workload'
    });

    // 添加震动反馈
    wx.vibrateShort({
      type: 'light'
    });

    console.log('显示工作量弹窗');
  },

  // 隐藏工作量弹窗
  hideWorkloadPopup: function () {
    this.setData({
      showWorkloadPopup: false
    });

    console.log('隐藏工作量弹窗');
  },

  // 添加新活动 
  addNewActivity: function () {
    wx.navigateTo({
      url: '/pages/activity/add/add'
    });
  }
}) 